package com.ztgf.commodity.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ztgf.commodity.models.dto.CommodityCategoryDTO;
import com.ztgf.commodity.models.dto.CommodityParamValueDTO;
import com.ztgf.commodity.models.service.ICommodityCategoryService;
import com.ztgf.commodity.models.service.ICommodityParamValueService;
import com.ztgf.commodity.models.vo.CommodityCategoryParamRelationVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;

import com.ztgf.commodity.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.commodity.mapper.CommodityCategoryParamRelationMapper;
import com.ztgf.commodity.models.service.ICommodityCategoryParamRelationService;
import com.ztgf.commodity.models.bo.CommodityCategoryParamRelationSaveBO;
import com.ztgf.commodity.models.dto.CommodityCategoryParamRelationDTO;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 商品分类参数关系表控制器
 *
 * @author chenyu.wang
 * @Date 2020-06-15 10:30:06
 */
@DubboService
public class CommodityCategoryParamRelationServiceImpl extends ServiceImpl<CommodityCategoryParamRelationMapper, CommodityCategoryParamRelationDTO> implements ICommodityCategoryParamRelationService {

    @Resource
    private YmlConfig ymlConfig;
    @Autowired
    private ICommodityCategoryService categoryService;
    @Autowired
    private ICommodityParamValueService paramValueService;

    @Override
    public boolean add(CommodityCategoryParamRelationSaveBO saveBO) {
        CommodityCategoryDTO commodityCategory = categoryService.selectById(saveBO.getCategoryId());
        if (commodityCategory != null) {
            if (commodityCategory.getParentId() == 0) {
                CommodityCategoryParamRelationDTO dto = new CommodityCategoryParamRelationDTO();
                BeanUtils.copyProperties(saveBO, dto);
                Long id = ymlConfig.getPkId();
                dto.setId(id);
                return this.insert(dto);
            }
        }
        return false;
    }

    @Override
    public List<CommodityCategoryParamRelationVO> queryList(Long categoryId, Long brandId, String filterName) {
        List<CommodityCategoryParamRelationVO> list = baseMapper.queryCategoryParamList(categoryId, brandId, filterName);
        if (list != null && list.size() > 0) {
            for (CommodityCategoryParamRelationVO vo : list) {
                //查询参数值信息
                List<CommodityParamValueDTO> paramValueList = paramValueService.queryListByParamId(vo.getParamId());
                vo.setParamList(paramValueList);
            }
        }
        return list;
    }

    @Override
    public void addList(Long userId, Long categoryId, String ids) {
        CommodityCategoryParamRelationDTO dto;
        List<CommodityCategoryParamRelationDTO> insertList = new ArrayList<>();
        String[] id = ids.split(",");
        List<String> list = Arrays.asList(id);
        if (list.size() > 0) {
            for (String s : list) {
                dto = new CommodityCategoryParamRelationDTO();
                dto.setId(ymlConfig.getPkId());
                dto.setCreateUserId(userId);
                dto.setCategoryId(categoryId);
                dto.setParamId(Long.valueOf(s));
                insertList.add(dto);
            }
            this.insertBatch(insertList);
        }
    }

    @Override
    public boolean deleteAccordingToCategoryId(Long categoryId) {
        Wrapper<CommodityCategoryParamRelationDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("category_id", categoryId);
        return this.delete(wrapper);
    }
}
